package pkg.zomb;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.widget.Toast;

public class zomb extends BaseGameActivity implements IOnSceneTouchListener{
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	private static final float DEMO_VELOCITY = 250.0f;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private Texture mTexture;
	private TextureRegion mSurvivorTextureRegion;
	private TextureRegion mZombieTextureRegion;
	public float lastTouchX = 0;
	public float lastTouchY = 0;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		//this might be useful
		//Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));		
	}

	@Override
	public void onLoadResources() {
		this.mTexture = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mSurvivorTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/face_box.png", 0, 0);
		this.mZombieTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/face_box2.png", 32, 0);	

		this.mEngine.getTextureManager().loadTexture(this.mTexture);
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
		scene.setOnSceneTouchListener(this);

		/* Calculate the coordinates for the face, so its centered on the camera. */
//		final int centerX = (CAMERA_WIDTH - this.mSurvivorTextureRegion.getWidth()) / 2;
//		final int centerY = (CAMERA_HEIGHT - this.mSurvivorTextureRegion.getHeight()) / 2;

		/* Create the face and add it to the scene. */
		//final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion);
//		final Sprite survivor = new Survivor(centerX, centerY, this.mSurvivorTextureRegion);
		
//		scene.getLastChild().attachChild(survivor);
		createSurvivor(scene);
		createZombie(scene);

		return scene;
	}

	@Override
	public void onLoadComplete() {

	}

	// ===========================================================
	// Methods
	// ===========================================================
	private void createSurvivor(Scene s) {
		final int centerX = (CAMERA_WIDTH - this.mSurvivorTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mSurvivorTextureRegion.getHeight()) / 2;
		final Sprite survivor = new Survivor(centerX, centerY, this.mSurvivorTextureRegion);

		s.getLastChild().attachChild(survivor);
	}
	private void createZombie(Scene s) {
	        final BasicZombie z = new BasicZombie(100, 100, mZombieTextureRegion);
	        s.getLastChild().attachChild(z);
	        // This was in the tutorial: http://www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html
	        // but it does not work.
	        //this.mEngine.getScene().getLastChild().attachChild(z);
	}
	private void createZombie(Scene s, float x, float y) {
        final BasicZombie z = new BasicZombie(x, y, mZombieTextureRegion);
        s.getLastChild().attachChild(z);
	}

	public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
		if(pSceneTouchEvent.isActionDown()) {
			//createZombie(pScene, pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
			lastTouchX = pSceneTouchEvent.getX();
			lastTouchY = pSceneTouchEvent.getY();
			return true;
		}
		return false;
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	private class Survivor extends Sprite {
		
        public Survivor(final float pX, final float pY,
                final TextureRegion pTextureRegion) {        	
            super(pX, pY, pTextureRegion);
        }
        
        @Override
        protected void onManagedUpdate(final float pSecondsElapsed) {
/*
            if (this.mX < 0) {
                this.setVelocityX(DEMO_VELOCITY);
            } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
                this.setVelocityX(-DEMO_VELOCITY);
            }
 
            if (this.mY < 0) {
                this.setVelocityY(DEMO_VELOCITY);
            } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
                this.setVelocityY(-DEMO_VELOCITY);
            }
//*/
        	if(this.mX < lastTouchX)
        		mX+=2;
        	if(this.mX > lastTouchX)
        		mX-=2;
        	if(this.mY < lastTouchY)
        		mY+=2;
        	if(this.mY > lastTouchY)
        		mY-=2;
        	 
            super.onManagedUpdate(pSecondsElapsed);
        }
    }
}